import { $wuxActionSheet } from '../../../dist/index';

var e = getApp(), a = e.require("utils/util.js"), t = e.require("utils/api.js"), n = e.require("utils/enums.js"), r = e.require("utils/onfire.js"), i = null, s = a.makeArray(1, 24), d = [], o = [], u = null, l = [[], []], p = {
  MinFreightMoney: 2
}, f = n.ErrandTypes[3], h = !1, g = !1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBusy: !1,
    dialogType: null,
    remarkInputFocus:!1,

    addresses: d,
    addressAdd: !1, 

    moneyPayable: p.MinFreightMoney,

    deliveryTimes: l,
    deliveryTimeIndex: [0, 0],
    deliveryTimeValue: {
      text: "即刻出发",
      value: null
    },
    errandGender:'',
    expiredArr:[],

    weightsArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var expiredArr = this.getExpiredValue();
    var weightsArr = this.getWeightsValue();
    i = new Date();
    var l = this.getPickerTimes('0','8:00','22:00');
    this.onErrandTypeChange(options);
    this.setData({
      deliveryTimes: l,
      weightsArr: weightsArr,
      expiredArr: expiredArr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var a = this;
    e.callAuthorize(a, function () {
      g = !0, a.setData({
        remarkInputFocus: h && g && !u.type
      });
    });
  },

  // 地址栏
  onErrandTypeChange: function (e) {
    var t = this, r = f.value, i = a.indexOfArray(n.ErrandTypes, function (e) {
      return r == e.value;
    }), s = !1;
    if (d.splice(0, d.length), i == n.ErrandTypes.length - 1) s = !0, d.push({
      self: null,
      text: "地点在哪里",
      address: null
    }); else switch (i) {
      case 0:
        d.push({
          self: !1,
          text: "在哪里购买（可选）",
          address: null
        }), d.push({
          self: !0,
          text: "送到哪里去",
          address: p.AddressInfo
        });
        break;

      case 1:
        d.push({
          self: !1,
          text: "在哪里取物品（可选）",
          address: null
        }), d.push({
          self: !0,
          text: "送到哪里去",
          address: p.AddressInfo
        });
        break;

      case 2:
        d.push({
          self: !0,
          text: "在哪里取物品",
          address: p.AddressInfo
        }), d.push({
          self: !1,
          text: "寄到哪里去",
          address: null
        });
        break;

      case 3:
        d.push({
          self: !1,
          text: "到哪里上课",
          address: null
        });
    }
    t.setData({
      addressAdd: s,
      addresses: d,
      errandType: n.ErrandTypes[i]
    });
  },

  onAddressAddTap: function (e) {
    var a = this, t = e.currentTarget.dataset.index;
    void 0 !== t && (t == d.length - 1 ? d.push({
      self: !1,
      text: "然后到哪里去",
      address: null
    }) : (0 == t && (d[t + 1].text = d[t].text), d.splice(t, 1)), a.setData({
      addresses: d
    }));
  },  
  
  // 监听订单价格
  numberChange:function(e){
    // console.log(e);
    this.setData({
      moneyPayable:e.detail.value
    });
  },

  // 跳转红包列表页面
  toCoupon: function () {
    wx.navigateTo({
      url: '../../user/coupon/index',
    })
  },

  // 选择物品重量
  weightsPickerChange: function (e) {
    var t = this;
    t.setData({
      weightsValue: t.data.weightsArr[e.detail.value]
    })
  },

  // 订单限制弹出框
  showActionSheet() {
    var t = this;
    if (this.timeout) clearTimeout(this.timeout)
    const hideSheet = $wuxActionSheet().showSheet({
      theme: 'wx',
      buttons: n.ErrandGenderLimits,
      buttonClicked(index, item) {
        t.setData({
          errandGender: item
        })
        return true
      },
    })
    this.timeout = setTimeout(hideSheet, 3000)
  },

  // 订单过期时间选择
  expirePickerChange:function(e){
    var t = this;
    t.setData({
      expiredValue: t.data.expiredArr[e.detail.value]
    })
  },

  // 立即授权
  onAuthorizeCallback: function (a) {
    var t = this;
    if (a.detail.userInfo) {
      t.setData({
        dialogType: null
      });
      e.authorizeCallback(t, a.detail.userInfo);
    }
  },

  getPickerTimes: function (e, t, n) {
    var r = [[], []];
    t = t || "00:00", n = n || "23:30", r[0].push({
      text: "今天",
      value: 0
    });
    for (v = 0; v < 2; ++v) {
      var s = 0 == v ? "明天" : 1 == v ? "后天" : "";
      r[0].push({
        text: (s.length ? s + "（" : s) + a.formatDate(a.addDate(i, v + 1)) + (s.length ? "）" : s),
        value: v + 1
      });
    }
    var d = null;
    if (e) {
      var o = a.addDate(i, 1), l = parseInt(t.split(":")[0]), f = parseInt(t.split(":")[1]);
      d = new Date(o.getFullYear(), o.getMonth(), o.getDate(), l, f, 0);
    } else {
      d = a.addDate(i, 30 * (4 == u.type ? 1 : 2), "m"), r[1].push({
        text: "即刻出发",
        value: null
      });
      var h = 30 - d.getMinutes() % 30;
      d = a.addDate(a.addDate(d, h, "m"), -d.getSeconds(), "s");
    }
    for (var g = parseInt(n.split(":")[0]), m = parseInt(n.split(":")[1]), c = new Date(d.getFullYear(), d.getMonth(), d.getDate(), g, m, 0), y = d, v = 0; y < c; v++) y = a.addDate(d, 30 * v, "m"),
      r[1].push({
        text: a.formatDate(y, "minute", "time"),
        value: y
      });
    return r;
  },

  // 时间选择后确认操作
  onDeliveryTimeChange: function (e) {
    console.log(e);
    // var a = this, t = e.detail.value[0], n = e.detail.value[1];
    // l[0][t], l[1][e.detail.value[1]];
    // a.setData({
    //   deliveryTimeValue: {
    //     text: (0 == t && 0 == n ? "" : l[0][t].text + " ") + l[1][n].text,
    //     value: l[1][n].value
    //   }
    // });
  },

  // 上拉框选择过程触发事件
  onDeliveryTimeColumnChange: function (e) {
    console.log(e);
    // if (0 == e.detail.column) {
    //   var a = this, t = a.getPickerTimes(e.detail.value, a.data.model.ErrandTimeRangeBegin || "8:00", a.data.model.ErrandTimeRangeEnd || "22:00");
    //   l.splice(0, l.length), l.push(t[0]), l.push(t[1]), a.setData({
    //     deliveryTimes: l
    //   });
    // }
  },

  getExpiredValue:function(){
    for(var o = [], i = 24; i > 0; i--) o.push(i+'小时');
    return o;
  },

  getWeightsValue:function(){
    var array = n.ErrandWeights;
    for(var o =[], i = 0; i < array.length; i++) o.push(array[i].text);
    return o;
  }
})